﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;

namespace mrp16062010.Serialize
{
    class SoapSerialization
    {
        public static void SoapSerialize(Object obj, string filePath)
        {
            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(filePath, FileMode.Create);
                SoapFormatter b = new SoapFormatter();
                b.Serialize(fileStream, obj);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }
        }
        public static T SoapSerialize<T>(string filePath)
        {
            FileStream fileStream = null;
            Object obj;
            try
            {
                if (File.Exists(filePath) == false)
                    throw new FileNotFoundException("The file" +
                              " was not found.", filePath);
                fileStream = new FileStream(filePath, FileMode.Open);
                SoapFormatter b = new SoapFormatter();
                obj = b.Deserialize(fileStream);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }
            return (T)obj;
        }
    }
}
